package com.freshfresh.adapter;

import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.freshfresh.activity.R;
import com.freshfresh.activity.classification.ProductDetailsActivity;
import com.freshfresh.activity.home.HomeActivity;
import com.freshfresh.utils.ImageLoadOptions;
import com.freshfresh.view.autoscroll.Imageloader_homePager;
import com.freshfresh.view.autoscroll.TagViewPager;
import com.freshfresh.view.autoscroll.TagViewPager.OnGetView;
import com.nostra13.universalimageloader.core.ImageLoader;


public class FirstListViewAdapter extends BaseAdapter
{
  private HomeActivity ac;
  private List<Map<String,Object>> data;
  private List<Map<String,Object>> urlBeans;

  public FirstListViewAdapter(HomeActivity paramMainActivity, List<Map<String,Object>> data,List<Map<String,Object>> urlBeans)
  {
    this.ac = paramMainActivity;
    this.data = data;
    this.urlBeans = urlBeans;
  }

  public int getCount()
  {
    return this.data.size();
  }

  public Map<String,Object> getItem(int paramInt)
  {
    return (Map<String,Object>)this.data.get(paramInt);
  }

  public long getItemId(int paramInt)
  {
    return paramInt;
  }

  public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
  {
    if (paramInt == 0)
      return getBannerView();
//    	return new IndexFourQuick().getView(this.ac);
    if (paramInt == 1)
      return new IndexFourQuick().getView(this.ac);
    if (paramInt <4){
    	return new IndexListItem().getView(this.ac, getItem(paramInt));
    }
    
    if (paramInt < 7){
      return new IndexListThreeItem().getView(this.ac, getItem(paramInt));
    }
//    return new IndexListItemEnd().getView(this.ac, getItem(paramInt));
    View vv = ac.getLayoutInflater().inflate(R.layout.index_list_item3, null);
	return new IndexListItemEnd().getView(this.ac, getItem(paramInt));
  }
  
  public View getBannerView(){
	  View vv=ac.getLayoutInflater().inflate(R.layout.xichehome, null);
	  TagViewPager  viewPager = (TagViewPager) vv.findViewById(R.id.myviewpager);
	  
	  
	  viewPager.init(R.drawable.indicator_focused,
				R.drawable.indicator, 10, 5, 2, 20);
		viewPager.setAutoNext(true, 2000);
		viewPager.setOnGetView(new OnGetView() {

			@Override
			public View getView(ViewGroup container,
					int position) {
				// TODO Auto-generated method stub
				ImageView iv = new ImageView(
						ac);
				iv.setScaleType(ScaleType.FIT_XY);
				
				
				ImageLoader.getInstance().displayImage(
						urlBeans.get(position).get("banner_url").toString(), iv,
						ImageLoadOptions.getOptions(0));
				
				final int pos=position;
				iv.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						Intent intent=new Intent(ac,ProductDetailsActivity.class);
						intent.putExtra("productid", urlBeans.get(pos).get("productid").toString());
						intent.putExtra("categoryid", urlBeans.get(pos).get("categories").toString());
						intent.putExtra("sku", "");
						ac.startActivity(intent);
					}
				});
				container.addView(iv);
				return iv;
			}
		});
		viewPager.setAdapter(urlBeans.size());
	  return vv;
  }
  
}